package com.simple.util.base.extend;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * @program: simple_tools
 * @description: 地区工具类
 * @author: ChenWenLong
 * @create: 2019-06-04 13:53
 **/
public class LocaleUtil {

    //用于存储不可修改的区域列表
    private static final List cAvailableLocaleList;
    //去重的区域集合
    private static Set cAvailableLocaleSet;
    //按照语言-国家的键值对存储的map集合
    private static final Map cLanguagesByCountry = Collections.synchronizedMap(new HashMap());
    //按照国家-语言的键值对存储的map集合
    private static final Map cCountriesByLanguage = Collections.synchronizedMap(new HashMap());
    static {
        List list = Arrays.asList(Locale.getAvailableLocales());
        cAvailableLocaleList = Collections.unmodifiableList(list);
    }

    public LocaleUtil() {
        super();
    }

    /**
     * 功能描述:
     * 〈将字符串转换成地区代码〉
     *
     * @params : [str]
     * @return : java.util.Locale
     * @author : cwl
     * @date : 2019/6/4 13:58
     */
    public static Locale toLocale(String str) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len != 2 && len != 5 && len < 7) {
            throw new IllegalArgumentException("Invalid locale format: " + str);
        }
        char ch0 = str.charAt(0);
        char ch1 = str.charAt(1);
        if (ch0 < 'a' || ch0 > 'z' || ch1 < 'a' || ch1 > 'z') {
            throw new IllegalArgumentException("Invalid locale format: " + str);
        }
        if (len == 2) {
            return new Locale(str, "");
        } else {
            if (str.charAt(2) != '_') {
                throw new IllegalArgumentException("Invalid locale format: " + str);
            }
            char ch3 = str.charAt(3);
            if (ch3 == '_') {
                return new Locale(str.substring(0, 2), "", str.substring(4));
            }
            char ch4 = str.charAt(4);
            if (ch3 < 'A' || ch3 > 'Z' || ch4 < 'A' || ch4 > 'Z') {
                throw new IllegalArgumentException("Invalid locale format: " + str);
            }
            if (len == 5) {
                return new Locale(str.substring(0, 2), str.substring(3, 5));
            } else {
                if (str.charAt(5) != '_') {
                    throw new IllegalArgumentException("Invalid locale format: " + str);
                }
                return new Locale(str.substring(0, 2), str.substring(3, 5), str.substring(6));
            }
        }
    }

    /**
     * 功能描述:
     * 〈将Locale地区转换成Locale地区集合〉
     *
     * @params : [locale]
     * @return : java.util.List
     * @author : cwl
     * @date : 2019/6/4 14:00
     */
    public static List localeLookupList(Locale locale) {
        return localeLookupList(locale, locale);
    }

    /**
     * 功能描述:
     * 〈将locale地区转换成地区集合,默认可以为defaultLocale〉
     *
     * @params : [locale, defaultLocale]
     * @return : java.util.List
     * @author : cwl
     * @date : 2019/6/4 14:01
     */
    public static List localeLookupList(Locale locale, Locale defaultLocale) {
        List list = new ArrayList(4);
        if (locale != null) {
            list.add(locale);
            if (locale.getVariant().length() > 0) {
                list.add(new Locale(locale.getLanguage(), locale.getCountry()));
            }
            if (locale.getCountry().length() > 0) {
                list.add(new Locale(locale.getLanguage(), ""));
            }
            if (list.contains(defaultLocale) == false) {
                list.add(defaultLocale);
            }
        }
        return Collections.unmodifiableList(list);
    }

    /**
     * 功能描述:
     * 〈获取当前有效的地区集合〉
     *
     * @params : []
     * @return : java.util.List
     * @author : cwl
     * @date : 2019/6/4 14:02
     */
    public static List availableLocaleList() {
        return cAvailableLocaleList;
    }

    /**
     * 功能描述:
     * 〈获取当前有效的地区set集合〉
     *
     * @params : []
     * @return : java.util.Set
     * @author : cwl
     * @date : 2019/6/4 14:02
     */
    public static Set availableLocaleSet() {
        Set set = cAvailableLocaleSet;
        if (set == null) {
            set = new HashSet(availableLocaleList());
            set = Collections.unmodifiableSet(set);
            cAvailableLocaleSet = set;
        }
        return set;
    }

    /**
     * 功能描述:
     * 〈校验是否是当前有效的地区集合〉
     *
     * @params : [locale]
     * @return : boolean
     * @author : cwl
     * @date : 2019/6/4 14:03
     */
    public static boolean isAvailableLocale(Locale locale) {
        return availableLocaleList().contains(locale);
    }

    /**
     * 功能描述:
     * 〈获取countryCode国家代码对应的语言代码集合〉
     *
     * @params : [countryCode]
     * @return : java.util.List
     * @author : cwl
     * @date : 2019/6/4 14:03
     */
    public static List languagesByCountry(String countryCode) {
        List langs = (List) cLanguagesByCountry.get(countryCode);  //syncd
        if (langs == null) {
            if (countryCode != null) {
                langs = new ArrayList();
                List locales = availableLocaleList();
                for (int i = 0; i < locales.size(); i++) {
                    Locale locale = (Locale) locales.get(i);
                    if (countryCode.equals(locale.getCountry()) &&
                            locale.getVariant().length() == 0) {
                        langs.add(locale);
                    }
                }
                langs = Collections.unmodifiableList(langs);
            } else {
                langs = Collections.EMPTY_LIST;
            }
            cLanguagesByCountry.put(countryCode, langs);  //syncd
        }
        return langs;
    }

    /**
     * 功能描述:
     * 〈获取该语言languageCode代码对应的国家代码〉
     *
     * @params : [languageCode]
     * @return : java.util.List
     * @author : cwl
     * @date : 2019/6/4 14:04
     */
    public static List countriesByLanguage(String languageCode) {
        List countries = (List) cCountriesByLanguage.get(languageCode);  //syncd
        if (countries == null) {
            if (languageCode != null) {
                countries = new ArrayList();
                List locales = availableLocaleList();
                for (int i = 0; i < locales.size(); i++) {
                    Locale locale = (Locale) locales.get(i);
                    if (languageCode.equals(locale.getLanguage()) &&
                            locale.getCountry().length() != 0 &&
                            locale.getVariant().length() == 0) {
                        countries.add(locale);
                    }
                }
                countries = Collections.unmodifiableList(countries);
            } else {
                countries = Collections.EMPTY_LIST;
            }
            cCountriesByLanguage.put(languageCode, countries);  //syncd
        }
        return countries;
    }
}
